package com.yiqi.liebang.feature.home.view;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.SearchFriendBo;
import com.yiqi.liebang.feature.home.contract.FriendsContract;
import com.yiqi.liebang.feature.home.dependencies.friends.DaggerFriendsComponent;
import com.yiqi.liebang.feature.home.dependencies.friends.FriendsPresenterModule;
import com.yiqi.liebang.feature.home.view.adapter.ChoiceExpertAdpter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchExpertActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, FriendsContract.View, TextView.OnEditorActionListener, BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.rv_expert)
    RecyclerView mRvExpert;
    ChoiceExpertAdpter mAdapter = null;
    @BindView(R.id.btn_back)
    ImageView mBtnBack;
    @BindView(R.id.edt_expert)
    EditText mEdtExpert;
    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<SearchFriendBo.UserFriendBean> mData;
    private String content;


    @Inject
    FriendsContract.Presenter mPresenter;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_search_expert;
    }

    @Override
    protected void initInjector() {
        DaggerFriendsComponent.builder().friendsPresenterModule(new FriendsPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initAdapter();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        mEdtExpert.setOnEditorActionListener(this);
    }


    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        mAdapter.changeState(position);
        Intent intent = new Intent();
        intent.putExtra("three", mData.get(position));
        setResult(2, intent);
        finish();
    }


    @Override
    public void showFriendsListSuccess(List<SearchFriendBo.UserFriendBean> userFriendBeans) {
        mData = userFriendBeans;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showFriendsListError(String error) {
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showFriendsListMoreSuccess(List<SearchFriendBo.UserFriendBean> userFriendBeans) {
        setData(false, userFriendBeans);
    }

    @Override
    public void showFriendsListMoreError(String msg) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void showFriendsListEmpty() {
        mAdapter.setEmptyView(mEmptyView.getEmptyView("暂无搜索到匹配的行家"));
        mAdapter.setNewData(null);
    }

    @Override
    public void showPeopleListSuccess(List<SearchFriendBo.UserFriendBean> workBoPageData) {

    }

    @Override
    public void showListEmpty() {

    }
    private void loadMore() {
        mPresenter.searchFriendMore(content, pageIndex, pageSize);
    }

    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }


    }
    private void initAdapter() {
        mEmptyView = new EmptyView(mRvExpert);
        mRvExpert.setLayoutManager(new LinearLayoutManager(this));
        mRvExpert.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapter = new ChoiceExpertAdpter();
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mAdapter.bindToRecyclerView(mRvExpert);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setEmptyView(mEmptyView.getEmptyView("请输入搜索关键词！"));
        mAdapter.setNewData(null);
        mAdapter.setOnLoadMoreListener(this, mRvExpert);
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            content = mEdtExpert.getText().toString();
            if (TextUtils.isEmpty(content)) {
                showMsg("请输入搜索内容！");
                return true;
            }
            pageIndex = 1;
            mPresenter.searchExpert(content, pageIndex, pageSize);
        }
        return false;
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

}
